//
//  RadioMuluViewController.m
//  时光
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RadioMuluViewController.h"
#import "RadioPlayViewController.h"
#import "RadioSecondTableViewCell.h"
#import "RadioSecondModel.h"

@interface RadioMuluViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, copy) NSString *playlist_small_image;
@property (nonatomic, copy) NSString *playlist_name;
@property (nonatomic, copy) NSString *pimg;
@property (nonatomic, copy) NSString *username;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *imgArray;



@end

@implementation RadioMuluViewController

-(NSMutableArray *)array{
    if (_array == nil) {
        
        _array = [NSMutableArray array];
    }
    return _array;
}

-(NSMutableArray *)imgArray{
    if (_imgArray == nil) {
        _imgArray = [NSMutableArray array];
    }
    return _imgArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
    [self creatView];
    [self creatHeaderView];
    [self getData];
}

- (void)creatView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;  // 分割线
    
    [self.tableView registerClass:[RadioSecondTableViewCell class] forCellReuseIdentifier:@"RadioSecondTableViewCell"];
    self.tableView.rowHeight = 100;
}

- (void)creatHeaderView{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 350 * H)];
    [self.view addSubview:self.headView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 230 * H)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.playlist_small_image] placeholderImage:[UIImage imageNamed:@"1920329c2e54f271d719def0d6f179fe.jpg"]];
    [self.headView addSubview:imageView];
    
    UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 230 * H, WIDTH, 30 * H)];
    labelName.text = self.playlist_name;
    labelName.font = [UIFont systemFontOfSize:20 * H weight:0.1];
    [self.headView addSubview:labelName];
    
    UIImageView *imageViewSign = [[UIImageView alloc] initWithFrame:CGRectMake(10 * W, 265 * H, 25 * W, 25 * W)];
    [imageViewSign sd_setImageWithURL:[NSURL URLWithString:self.pimg] placeholderImage:[UIImage imageNamed:@"iconfont-erji"]];
    imageViewSign.layer.cornerRadius = 10 * W;
    imageViewSign.layer.masksToBounds = YES;
    [self.headView addSubview:imageViewSign];
    
    UILabel *labelSign = [[UILabel alloc] initWithFrame:CGRectMake(35 * W, 265 * H, 150 * W, 25 * H)];
    labelSign.font = [UIFont systemFontOfSize:15 * H];
    labelSign.textColor = [UIColor grayColor];
    labelSign.text = self.username;
    [self.headView addSubview:labelSign];
    
    UILabel *labelLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 295 * H, WIDTH, 1)];
    labelLine.backgroundColor = [UIColor grayColor];
    labelLine.alpha = 0.5;
    [self.headView addSubview:labelLine];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 300 * H, 100 * W, 25 * H)];
    label.text = @"最近听过";
    label.textColor = [UIColor grayColor];
    [self.headView addSubview:label];
    
    for (int i = 0; i < self.imgArray.count; i++) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((110 + (30 * i)) * W, 300 * H, 25 * W, 25 * W)];
        [imageV sd_setImageWithURL:[NSURL URLWithString:self.imgArray[i]]];
        imageV.layer.cornerRadius = 25 / 2 * W;
        imageV.layer.masksToBounds = YES;
        [self.headView addSubview:imageV];
    }
    
    UILabel *labelkong = [[UILabel alloc] initWithFrame:CGRectMake(0, 340 * H, WIDTH, 10 * H)];
    labelkong.alpha = 0.5;
    labelkong.backgroundColor = [UIColor grayColor];
    [self.headView addSubview:labelkong];
    
    self.tableView.tableHeaderView = self.headView;
}

- (void)getData{
    
    NSString *url = @"http://wawa.fm:9090/wawa/api.php/user/playlistinfo/";
    NSDictionary *header = @{@"Content-Length" : @"28", @"Content-Type" : @"application/x-www-form-urlencoded", @"Host" : @"wawa.fm:9090"};
    NSDictionary *body = @{@"platform" : @"wwandroid", @"uid" :  [NSString stringWithFormat:@"%@", self.ID]};
    
    [MHNetWorkTask numberPostWithURL:url withBody:body withBodyType:BodyTypeString withHttpHeader:header withResponseType:ResponseTypeJSON withSuccess:^(id result) {
        
        self.username = [result objectForKey:@"username"];
        self.pimg = [result objectForKey:@"pimg"];
        self.playlist_name = [result objectForKey:@"playlist_name"];
        self.playlist_small_image = [result objectForKey:@"playlist_small_image"];
        NSArray *listen = [result objectForKey:@"listen"];
        for (NSDictionary *dic in listen) {
            NSString *url = [dic objectForKey:@"pimg"];
            [self.imgArray addObject:url];
        }
        NSArray *tracks = [result objectForKey:@"tracks"];
        for (NSDictionary *dic in tracks) {
            RadioSecondModel *model = [[RadioSecondModel alloc] init];
            model.songer = [dic objectForKey:@"songer"];
            model.songname = [dic objectForKey:@"songname"];
            model.thumbnail_url = [dic objectForKey:@"thumbnail_url"];
            model.filename = [dic objectForKey:@"filename"];
            [self.array addObject:model];
        }
        [self.tableView reloadData];
        [self creatHeaderView];
    } withFail:^(NSError *error) {
        NSLog(@"=======");
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioSecondTableViewCell"];
    RadioSecondModel *model = self.array[indexPath.row];
    cell.model = model;
       return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioPlayViewController *player = [RadioPlayViewController sharePlayMusic];
    player.array = [self.array mutableCopy];
    player.index = indexPath.row;
   [self.navigationController pushViewController:player animated:YES];
  
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    SAND
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
